﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Shopping.BLL;
using Shopping.Common;
using Shopping.Model;

public partial class AS_ShoppingCartCommon : BasePage
{
    private readonly ShoppingCartsManage _shoppingCartManage = new ShoppingCartsManage();
    protected List<ShoppingCartInfo> _shoppingCart = new List<ShoppingCartInfo>();
    protected readonly GoodsManage _goodsManage = new GoodsManage();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string action = Request.QueryString["Action"] as string;
            switch (action)
            {
                case "ShowShoppingCart":   //
                    ShowShoppingCart();
                    break;
                case "delshopping":
                    DelShoppingCart();
                    break;
                case "DeleteAllShopping":
                    DeleteAllShopping();
                    break;
                case "ModifyQuantity":
                    ModifyQuantity();
                    break;
                case "DeleteCart":  //删除购物车中的指定商品；by 赵国伟 2012-08-19
                    DeleteCart();
                    break;
                case "AddCart":  //向购物车中添加指定的商品；by 赵国伟 2012-09-01
                    AddCart();
                    break;
            }

        }
        Response.End();
    }

    #region "向购物车中添加指定的商品 by 赵国伟 2012-09-01"
    /// <summary>
    /// 向购物车中添加指定的商品；
    /// </summary>
    private void AddCart()
    {
        int GoodsID = Utility.G<int>("GoodsID"); //获取商品ID;
        int PackUnitID = Utility.G<int>("PackUnitID"); //获取包装ID;
        int Quantity = Utility.G<int>("Quantity");
        if (Quantity <= 0)
            Quantity = 1;
        GoodsInfo g = _goodsManage.GetGoods(GoodsID);
        if (PackUnitID <= 0) PackUnitID = g.BaseInfo.PackUnitID;

        ShoppingCarts d = new ShoppingCarts();
        d.CreateDate = DateTime.Now;
        d.GoodsID = GoodsID;
        d.PackUnitID = PackUnitID;
        d.CookiesID = _shoppingCartManage.GetShoppingCartCookieName(this.UserID);
        d.Discount = 1.00M;
        d.PresentScores = g.BaseInfo.GiveScores * Quantity;
        d.Price = g.BaseInfo.SalePrice;
        d.Quantity = Quantity;
        ReturnValue ret =  _shoppingCartManage.Save(d);
        Response.Write(ret.ToJsonString());
    }
    #endregion


    #region "从购物车中删除指定的商品 by 赵国伟 2012-08-19"
    /// <summary>
    /// 从购物车中删除指定的商品
    /// </summary>
    private void DeleteCart()
    {
        int shoppingCartID = Utility.G<int>("shoppingCartID");
        Response.Write(_shoppingCartManage.Delete(_shoppingCartManage.GetShoppingCartCookieName(this.UserID), shoppingCartID).ToJsonString());
    }
    #endregion


    private void DeleteAllShopping()
    {
        ReturnValue result = _shoppingCartManage.Clear(_shoppingCartManage.GetShoppingCartCookieName(this.UserID));
        Response.Write(JSONHelper.ObjectToJson(result));
        Response.End();
    }
    private void DelShoppingCart()
    {
        int goodsid = Utility.P<int>("goodsid");
        int packUnitID = Utility.P<int>("PackUnitID");
        ReturnValue result = null;
        if (goodsid <= 0)
        {
            result = new ReturnValue(ResultType.Fail, "请选择要删除的商品");
        }
        else
        {
            result = _shoppingCartManage.Delete(_shoppingCartManage.GetShoppingCartCookieName(this.UserID), goodsid,packUnitID);
        }
        Response.Write(JSONHelper.ObjectToJson(result));
        Response.End();
    }
    private void ShowShoppingCart()
    {
        _shoppingCart = _shoppingCartManage.QueryList(_shoppingCartManage.GetShoppingCartCookieName(this.UserID));

    }

    #region "调整购物车指定商品的数量； by 赵国伟 2012-05-14"
    /// <summary>
    /// 调整购物车指定商品的数量；
    /// </summary>
    private void ModifyQuantity()
    {
        int ShoppingCartID = Utility.G<int>("ShoppingCartID");
        int Quantity = Utility.G<int>("Quantity");
        ReturnValue ret = _shoppingCartManage.ModifyQuantity(this.UserID, ShoppingCartID, Quantity);
        Response.Write(ret.ToJsonString());
        Response.End();
    }
    #endregion


}
